package com.py.article.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.py.apis.article.IArticleClient;
import com.py.article.service.ApArticleConfigService;
import com.py.article.service.ApArticleService;
import com.py.common.redis.CacheService;
import com.py.model.article.dtos.ArticleDto;
import com.py.model.article.pojos.ApArticleConfig;
import com.py.model.behavior.dtos.LikesBehaviorDto;
import com.py.model.behavior.dtos.ReadBehaviorDto;
import com.py.model.behavior.dtos.UnLikesBehaviorDto;
import com.py.model.common.dtos.ResponseResult;
import com.py.model.common.enums.AppHttpCodeEnum;
import com.py.model.moment.dtos.CommentSaveDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2022/11/11 9:56
 * @Version 1.0
 */
@RestController
@Slf4j
public class ArticleClient implements IArticleClient {


    @Autowired
    private ApArticleService articleService;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    /**
     * 可评论
     */
    private final static Integer IS_COMMENT = 1;
    /**
     * 已上架
     */
    private final static Integer IS_DOWN = 0;

    /**
     * 保存文章
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        log.info("收到参数:{}", dto);
        try {
            return articleService.saveArticle(dto);
        } catch (Exception e) {

        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 文章点赞
     *
     * @param dto
     * @return
     * @Author Axing
     */
    @Override
    public Integer articleLikes(LikesBehaviorDto dto) {
        log.info("ArticleClient·······");
        log.info("接收到LikesBehaviorDto：{}", dto);
        String likes = (String) cacheService.hGet(dto.getArticleId() + "", "likes");
        //获取当前用户id
        Integer userId = dto.getUserId();
        log.info("likes-->{}", likes);
        log.info("userId--->{}", userId);
        String userLikeKey = dto.getArticleId() + "_" + userId;


        Object uLikes = cacheService.hGet(userLikeKey, "likes");
        if (uLikes == null) {
            //初始化用户文章点赞关系
            cacheService.hPut(userLikeKey, "likes", "0");
        }

        Integer likesValue = null;
        if (likes != null) {
            likesValue = Integer.valueOf(likes);
        }
        if (likesValue == null) {
            likesValue = 0;
            cacheService.hPut(dto.getArticleId() + "", "likes", "0");
        }
        log.info("文章原点赞量：{}", likesValue);

        if (dto.getOperation() == 0) {
            //点赞
            log.info("点赞");
            cacheService.hIncrBy(dto.getArticleId() + "", "likes", 1);
            cacheService.hPut(userLikeKey, "likes", "1");
            likesValue++;
        } else {
            //取消点赞
            log.info("取消点赞");
            if (likesValue != 0) {
                cacheService.hIncrBy(dto.getArticleId() + "", "likes", -1);
                cacheService.hPut(userLikeKey, "likes", "0");
                likesValue--;
            }
        }
        log.info("文章现点赞量：{}", likesValue);
        return likesValue;
    }

    /**
     * 文章阅读量
     *
     * @param dto
     * @return
     */
    @Override
    public Integer articleRead(ReadBehaviorDto dto) {
        String readKey = dto.getArticleId() + "";
        String readNum = (String) cacheService.hGet(readKey, "read");
        if (readNum == null) {
            cacheService.hPut(readKey, "read", "0");
        }
        //获取当前用户id
        Integer userId = dto.getUserId();
        //用户阅读key
        String userReadKey = dto.getArticleId() + "_" + userId;
        String uReadNum = (String) cacheService.hGet(userReadKey, "read");
        if (uReadNum == null) {
            cacheService.hPut(userReadKey, "read", "0");
        }
        //新增文章阅读量
        cacheService.hIncrBy(readKey, "read", 1);
        //用户该文章阅读量
        cacheService.hIncrBy(userReadKey, "read", 1);
        readNum = (String) cacheService.hGet(readKey, "read");
        return Integer.valueOf(readNum);
    }

    /**
     * 文章不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public Integer articleUnLike(UnLikesBehaviorDto dto) {
        String unLikesKey = dto.getArticleId() + "";
        String unLikesNum = (String) cacheService.hGet(unLikesKey, "unLikesNum");
        if (unLikesNum == null) {
            cacheService.hPut(unLikesKey, "unLikesNum", "0");
        }
        //获取当前用户id
        Integer userId = dto.getUserId();
        String userUnLikesKey = dto.getArticleId() + "_" + userId;
        String uUnlikes = (String) cacheService.hGet(userUnLikesKey, "unLikes");
        if (uUnlikes == null) {
            //默认为喜欢
            cacheService.hPut(userUnLikesKey, "unLikes", "0");
        }
        if (dto.getType() == 0) {
            //文章不喜欢数量增加
            cacheService.hIncrBy(unLikesKey, "unLikesNum", 1);
            //用户置为不喜欢
            cacheService.hPut(userUnLikesKey, "unLikes", "1");
        } else {
            if (!"0".equals(cacheService.hGet(unLikesKey, "unLikesNum"))) {
                //文章不喜欢数量减少
                cacheService.hIncrBy(unLikesKey, "unLikesNum", -1);
            }
            //用户取消不喜欢
            cacheService.hPut(userUnLikesKey, "unLikes", "0");
        }
        String unlikes = (String) cacheService.hGet(unLikesKey, "unLikesNum");
        return Integer.valueOf(unlikes);
    }

    /**
     * 文章是否可评论
     *
     * @param commentSaveDto
     * @return
     */
    @Override
    public boolean query(CommentSaveDto commentSaveDto) {
        boolean flag = false;

        ApArticleConfig apArticleConfig = apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery()
                .eq(ApArticleConfig::getArticleId, commentSaveDto.getArticleId()));

        if (apArticleConfig.getIsComment() && !apArticleConfig.getIsDown()) {
            //可评论并且已上架
            flag = true;
        }
        return flag;
    }
}